package org.example.qqrobot.service.impl;

import org.example.qqrobot.context.BaseContext;
import org.example.qqrobot.mapper.MessageMapper;
import org.example.qqrobot.mapper.UserMapper;
import org.example.qqrobot.model.pojo.Invitation;
import org.example.qqrobot.model.pojo.Message;
import org.example.qqrobot.model.vo.InvitationIntoPageVO;
import org.example.qqrobot.model.vo.MessageIntoPageVO;
import org.example.qqrobot.model.vo.MessageMainVO;
import org.example.qqrobot.service.MessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<MessageMainVO> lookMyMessage() {
        List<MessageMainVO> mvList = new ArrayList<>();
        List<Message> mList = messageMapper.findByReceiverId(BaseContext.getCurrentId());

        for (int i = 0; i < mList.size(); i++) {
            MessageMainVO messageMainVO = new MessageMainVO();
            BeanUtils.copyProperties(mList.get(i),messageMainVO);
            messageMainVO.setSenderName(userMapper.getNameById(messageMapper.findSenderById(mList.get(i).getId())));

            mvList.add(messageMainVO);
        }

        return mvList;

    }

    @Override
    public MessageIntoPageVO intoPage(Long id) {
        MessageIntoPageVO messageIntoPageVO = new MessageIntoPageVO();

        Message message=  messageMapper.findById(id);

        BeanUtils.copyProperties(message,messageIntoPageVO);

        messageIntoPageVO.setSenderName(userMapper.getNameById(message.getSender()));
        message.setStatus(0);
        messageMapper.update(message);

        return messageIntoPageVO;

    }

    @Override
    public void already(Long id) {
        Message message = messageMapper.findById(id);
        message.setStatus(0);
        messageMapper.update(message);
    }
}
